package cn.xgxd.service.Impl;


import cn.hutool.core.util.StrUtil;
import cn.xgxd.mapper.UserMapper;
import cn.xgxd.service.UserService;
import cn.xgxd.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User search(User user){
        Map<String, Object> p = new HashMap<>();
        String username = user.getUsername();
        String password = user.getPassword();
        p.put("username",username);
        p.put("password",password);
        if (StrUtil.isBlank(username)||StrUtil.isBlank(password)){
            return null;
        }
        List<User> users = userMapper.selectByMap(p);
        User userone = users.stream().findFirst().orElse(null);
        return userone!=null?userone:null;
    }

    @Override
    public User search(Integer id) {
        User user = userMapper.selectById(id);
        return user!=null?user:null;
    }

    public boolean register( User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        QueryWrapper<User> wrapper2 = new QueryWrapper<>();
        String username = user.getUsername();
        String password = user.getPassword();
        String phone = user.getPhone();
        wrapper.eq("username", username);
        wrapper2.eq("phone", phone);
        User isUserName = userMapper.selectOne(wrapper);
        User isUserPhone = userMapper.selectOne(wrapper2);
        System.out.println(isUserPhone);
        if (isUserName!=null){
            return false;
        }
        else if (isUserPhone!=null){
            return false;
        }else {
            User u = new User();
            u.setUsername(username);
            u.setPassword(password);
            u.setPhone(phone);
            u.setAddress(user.getAddress());
            u.setName(user.getName());
            u.setAdmin(0);
            userMapper.insert(u);
            return true;
        }
    }

    @Override
    public int userTotal() {
        Integer usertotal = userMapper.selectCount(null);
        return usertotal;
    }

    @Override
    public User getUserById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public void upData(User user) {
        userMapper.updateById(user);
    }

    @Override
    public List<User> getAdmin() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("admin",1);
        List<User> users = userMapper.selectList(wrapper);
        return users;
    }

    @Override
    public List getUser() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("admin",0);
        List<User> users = userMapper.selectList(wrapper);
        return users;
    }

    @Override
    public void swAdmin(User user) {
        userMapper.updateById(user);
    }
}
